package org.xxmi.cors.common;

import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * 用于文件下载
 */
public class DownloadFile {
    private FileInputStream fileInputStream;
    private String fileName;
    private String suffix;
    private byte[] body;

    public FileInputStream getFileInputStream() {
        return fileInputStream;
    }

    public void setFileInputStream(FileInputStream fileInputStream) {
        this.fileInputStream = fileInputStream;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    /**
     * 获取后缀名
     * @return
     */
    public String getSuffixName(){
        if(!StringUtils.isEmpty(this.fileName)){
            return this.fileName.substring(this.fileName.lastIndexOf("."));
        }
        return "";
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    /**
     * 获取 byte
     * @return
     */
    public byte[] getFileBody(){
        byte[] body = null;
        if(this.fileInputStream != null){
            try {
                body = new byte[this.fileInputStream.available()];
                this.fileInputStream.read(body);
                this.fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return body;
    }
}
